package com.dayuanit.dy16.atm.controller;

import com.dayuanit.dy16.atm.dto.ResponseDTO;
import com.dayuanit.dy16.atm.entity.User;
import com.dayuanit.dy16.atm.exception.BizException;
import org.slf4j.Logger;
import org.slf4j.LoggerFactory;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.ExceptionHandler;

import javax.servlet.http.HttpSession;

public abstract class BaseController {
    protected static Logger logger = LoggerFactory.getLogger(UserController.class);

    protected static final String user_login_flag = "userLogin";

    @Autowired
    protected HttpSession httpSession;

    protected long getUserId() {
        User user = (User) httpSession.getAttribute(user_login_flag);
        if (null == user) {
            throw new BizException("请登录");
        }

        return user.getId();

//        return 1;
    }

    /**
     * 全局异常处理
     * @param be
     * @return
     */
    @ExceptionHandler(BizException.class)
    public ResponseDTO processBizException(BizException be) {
        logger.error(be.getMessage(), be);
        return ResponseDTO.fail(be.getMessage());
    }

    @ExceptionHandler(Exception.class)
    public ResponseDTO processException(Exception e) {
        logger.error(e.getMessage(), e);
        return ResponseDTO.fail("系统错误，请联系客服");
    }
}
